package com.markerhub.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.markerhub.common.dto.CourseDTO;
import com.markerhub.common.dto.CourseStatusDTO;
import com.markerhub.common.lang.Result;
import com.markerhub.common.vo.CourseVO;
import com.markerhub.common.vo.StudentVO;
import com.markerhub.entity.ManagerActivity;
import com.markerhub.entity.User;
import com.markerhub.service.CourseService;
import com.markerhub.service.ManagerActivityService;
import com.markerhub.service.ManagerClassService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teacher/course")
public class TeacherCourseController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private ManagerActivityService activityService;

    @Autowired
    private ManagerClassService classService;

    // 获取教师的课程列表
    @GetMapping("/list")
    @RequiresAuthentication
    public Result getCourseList(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String courseName,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String activityName) {

        // 获取当前登录教师ID
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Long teacherId = user.getId();

        IPage<CourseVO> pageData = courseService.getTeacherCourses(teacherId, currentPage, pageSize, courseName, status, activityName);
        return Result.succ(pageData);
    }

    // 创建或更新课程
    @PostMapping("/save")
    @RequiresAuthentication
    public Result saveCourse(@RequestBody @Validated CourseDTO courseDTO) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Long teacherId = user.getId();

        courseService.saveOrUpdateCourse(courseDTO, teacherId);
        return Result.succ("保存成功");
    }

    // 删除课程
    @DeleteMapping("/{id}")
    @RequiresAuthentication
    public Result deleteCourse(@PathVariable("id") Long id) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Long teacherId = user.getId();

        boolean success = courseService.deleteTeacherCourse(id, teacherId);
        return success ? Result.succ("删除成功") : Result.fail("删除失败");
    }

    // 更新课程状态
    @PutMapping("/status")
    @RequiresAuthentication
    public Result updateStatus(@RequestBody CourseStatusDTO statusDTO) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Long teacherId = user.getId();

        boolean success = courseService.updateCourseStatus(statusDTO.getCourseId(), statusDTO.getStatus(), teacherId);
        return success ? Result.succ("状态更新成功") : Result.fail("状态更新失败");
    }

    // 获取活动列表
    @GetMapping("/activities")
    @RequiresAuthentication
    public Result getActivities() {
        List<ManagerActivity> activities = activityService.list();
        return Result.succ(activities);
    }

    // 获取教学班学员列表
    @GetMapping("/class/students/{classId}")
    @RequiresAuthentication
    public Result getClassStudents(@PathVariable("classId") Long classId) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Long teacherId = user.getId();

        List<StudentVO> students = courseService.getClassStudents(classId, teacherId);
        return Result.succ(students);
    }
}